//
//  UserAccountViewModel.swift
//  weibo1
//
//  Created by Apple on 15/11/28.
//  Copyright © 2015年 Apple. All rights reserved.
//

import UIKit
import AFNetworking
import SVProgressHUD

class UserAccountViewModel: NSObject {
    
    //定义一个useraccount属性
    var account :UserAccount?
    
    override init() {
        super.init()
        //获取解挡数据
        account = UserAccount.loadAcouth()
    }
    
    //判断用户是否登录 ==nil的时候没有登录
    var userlogin :Bool{
        return account?.access_token != nil
    }
    
    var token :String? {
        return account?.access_token
    }
    
    var url :NSURL? {
        let strurl = NSURL(string: account?.avatar_large ?? "")
        return strurl
    }
    
    var name :String? {
    
        return account?.name
    }
    
    //请求token
    func setGainToken(code: String,finished:(error:NSError?) -> ()){
        let urlstring = "https://api.weibo.com/oauth2/access_token"
        
        let manager = AFHTTPSessionManager()
        let parameters = ["client_id":client_id,"client_secret":client_secret,"grant_type":"authorization_code","code":code,"redirect_uri":redirect_uri]
        
        manager.responseSerializer.acceptableContentTypes?.insert("text/plain")
        manager.POST(urlstring, parameters: parameters, success: { (_, result) -> Void in
            //        let token = result["access_token"] as! String
            //        let uid = result["uid"] as! String
            guard let dict = result as? [String : AnyObject] else{
                return
            }
            
            let account = UserAccount(dict: dict)
            //获取用户信息
            self.loadUserInfo(account ,finished: finished)
            
         
            
            }) { (_, error) -> Void in
                
                print(error)
                finished(error: error)
        }
        
        
    }
    
    //获取用户信息
    private func loadUserInfo(account:UserAccount,finished:(error :NSError?) -> ()){
        let urltring = "https://api.weibo.com/2/users/show.json"
        
        guard let access_token = account.access_token,userId = account.uid else {
            
            SVProgressHUD.showErrorWithStatus("获取网络数据失败,请稍后再试")
            return
        }
        let parameters = ["access_token": access_token, "uid": userId]

        let manager = AFHTTPSessionManager()
        //uid	false	int64	需要查询的用户ID。
        
//        let parameters = ["uid":account.uid,"access_token":account.access_token]
        manager.GET(urltring, parameters: parameters, success: { (_, result) -> Void in
            
            //        print(result)
            //        let avatar_large = result["avatar_large"]
            //        let name = result["name"]
            
            //赋值给模型数据
            account.avatar_large = result["avatar_large"] as? String
            account.name = result["name"] as? String
//            print(account)
            account.saveAcount()
            finished(error: nil)
            }) { (_, error) -> Void in
                
                print(error)
                finished(error: error)
        }
  }
}
